** For "Who Holds Sovereign Debt and Why It Matters" by Fang, Hardy, and Lewis
clear all
set more off
cd "~/Dropbox/Sovereign Bond Project/Replication_Package_submission_RFS"

global varlist Domestic_Total Foreign_Total Domestic_Bank Domestic_NonBank Domestic_CentralBank Foreign_Bank Foreign_NonBank Foreign_Official


************************************************************************
****                             Data                               ****
************************************************************************
import excel "Data/Paper_Data/FHL_Data_balance.xlsx", firstrow clear
destring Year, replace
drop if mi(Total_Debt, Domestic_Total, Domestic_CentralBank, Domestic_Bank, Domestic_NonBank, Foreign_Total, Foreign_Official, Foreign_Bank, Foreign_NonBank)
do "Code/00_Country_Definition.do"

distinct Country
tab Country
distinct Country if AE == 1 // 24
distinct Country if EM == 1 // 48
distinct Country if DC == 1 // 29
gen All=1

encode Country, generate(cid)
xtset cid Year
sort cid Year

foreach x in Total_Debt $varlist {
	gen d_`x' = d.`x'
	gen d_`x'_Tot = d_`x'/l.Total_Debt
}


************************************************************************
****                  Marginal Share Regressions                    ****
************************************************************************
foreach ctygroup in All AE EM DC {
	foreach x in $varlist {
	reghdfe d_`x'_Tot d_Total_Debt_Tot if `ctygroup' == 1, absorb(cid Year) vce(cluster cid)
	} 
}

************************************************************************
****                  Average Share Calculations                    ****
************************************************************************
foreach ctygroup in All AE EM DC {
	foreach x in $varlist {
		gen share_`x'_Tot = `x' / Total_Debt
		reg share_`x'_Tot if `ctygroup' == 1 // calculate mean for each group
		drop share_`x'_Tot
	}
}
